package com.jiangxiaofei.citycloud.activity.fleamarket;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.FleaMenuBean;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FleaConstant;
import com.jiangxiaofei.citycloud.mvp.FleaPersenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * @author: JiangXFei
 * @date: 2017/11/9 0009
 * @content: 选择分类页面
 */

public class FleaMarketCassifyActivity extends BaseActivity implements FleaConstant.GetFleaMenuView {
    @InjectView(R.id.recy_menu_one)
    RecyclerView recyMenuOne;
    @InjectView(R.id.recy_menu_two)
    RecyclerView recyMenuTwo;
    @InjectView(R.id.recy_menu_three)
    RecyclerView recyMenuThree;
    FleaConstant.GetFleanMenuPersenter getFleanMenuPersenter;
    private List<FleaMenuBean> fleaMenuOneBeans = new ArrayList<>();
    private List<FleaMenuBean> fleaMenuTwoBeans = new ArrayList<>();
    private List<FleaMenuBean> fleaMenuThreeBeans = new ArrayList<>();

    private int menuId = 0;
    private MenuAdapter menuOneAdapter;
    private MenuAdapter menuTwoAdapter;
    private MenuAdapter menuThreeAdapter;
    private Intent intent;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        setContentView(R.layout.activity_flea_classify_layout);
        ButterKnife.inject(this);
        initTitle();
        initRecyOneAdapter();
        initRecyTwoAdapter();
        initRecyThreeAdapter();
        intent = getIntent();
        getFleanMenuPersenter = new FleaPersenter(mContext, (FleaConstant.GetFleaMenuView) mActivity);
        getFleanMenuPersenter.getMainFleaMenu();
    }

    /**
     * 初始化总分类适配器
     */
    private void initRecyOneAdapter() {
        menuOneAdapter = new MenuAdapter(R.layout.item_flea_menu_classify, fleaMenuOneBeans);
        menuOneAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < fleaMenuOneBeans.size(); i++) {
                    fleaMenuOneBeans.get(i).setChecked(false);
                    if (i == position) {
                        fleaMenuOneBeans.get(i).setChecked(true);
                    }
                    menuOneAdapter.notifyDataSetChanged();
                }
                menuId = Integer.valueOf(fleaMenuOneBeans.get(position).getId());
                getFleanMenuPersenter.getFleaSencendMenu();
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyMenuOne.setLayoutManager(manager);
        recyMenuOne.setAdapter(menuOneAdapter);
    }

    /**
     * 初始化二级分类适配器
     */
    private void initRecyTwoAdapter() {
        menuTwoAdapter = new MenuAdapter(R.layout.item_flea_menu_classify, fleaMenuTwoBeans);
        menuTwoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < fleaMenuTwoBeans.size(); i++) {
                    fleaMenuTwoBeans.get(i).setChecked(false);
                    if (i == position) {
                        fleaMenuTwoBeans.get(i).setChecked(true);
                    }
                    menuTwoAdapter.notifyDataSetChanged();
                }
                menuId = Integer.valueOf(fleaMenuTwoBeans.get(position).getId());
                getFleanMenuPersenter.getFleaThreeMenu();
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyMenuTwo.setLayoutManager(manager);
        recyMenuTwo.setAdapter(menuTwoAdapter);
    }

    /**
     * 初始化三级分类适配器
     */
    private void initRecyThreeAdapter() {
        menuThreeAdapter = new MenuAdapter(R.layout.item_flea_menu_classify, fleaMenuThreeBeans);
        menuThreeAdapter.setOnItemClickListener((adapter, view, position) -> {
            for (int i = 0; i < fleaMenuThreeBeans.size(); i++) {
                fleaMenuThreeBeans.get(i).setChecked(false);
                if (i == position) {
                    fleaMenuThreeBeans.get(i).setChecked(true);

                }
                menuThreeAdapter.notifyDataSetChanged();
            }
            intent.putExtra(StringConstants.FLEATYPE, fleaMenuThreeBeans.get(position).getName());
            intent.putExtra(StringConstants.FLEATYPEID, Integer.valueOf(fleaMenuThreeBeans.get(position).getId()));
            mActivity.setResult(IntConstants.FLEARESPONSECODE, intent);
            mActivity.finish();
        });
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyMenuThree.setLayoutManager(manager);
        recyMenuThree.setAdapter(menuThreeAdapter);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("宝贝分类");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getFleaMenu() {
        return menuId;
    }

    @Override
    public void getFleaMainMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {
        fleaMenuOneBeans.clear();
        if (EmptyUtils.isNotEmpty(fleaMenuBeans)) {
            fleaMenuOneBeans.addAll(fleaMenuBeans);
            fleaMenuOneBeans.get(0).setChecked(true);
            menuId = Integer.valueOf(fleaMenuOneBeans.get(0).getId());
            getFleanMenuPersenter.getFleaSencendMenu();
        }
        menuOneAdapter.notifyDataSetChanged();
    }

    @Override
    public void getFleaSencendMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {
        fleaMenuTwoBeans.clear();
        if (EmptyUtils.isNotEmpty(fleaMenuBeans)) {
            fleaMenuTwoBeans.addAll(fleaMenuBeans);
            fleaMenuTwoBeans.get(0).setChecked(true);
            menuId = Integer.valueOf(fleaMenuTwoBeans.get(0).getId());
            getFleanMenuPersenter.getFleaThreeMenu();
        }
        menuTwoAdapter.notifyDataSetChanged();
    }

    @Override
    public void getFleaThreeMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {
        fleaMenuThreeBeans.clear();
        if (EmptyUtils.isNotEmpty(fleaMenuBeans)) {
            fleaMenuThreeBeans.addAll(fleaMenuBeans);
            fleaMenuThreeBeans.get(0).setChecked(true);
        }
        menuThreeAdapter.notifyDataSetChanged();
    }


    private class MenuAdapter extends BaseQuickAdapter<FleaMenuBean, BaseViewHolder> {

        public MenuAdapter(int layoutResId, @Nullable List<FleaMenuBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FleaMenuBean item) {
            helper.setText(R.id.tv_menu_one, item.getName());
            if (item.isChecked()) {
                helper.setTextColor(R.id.tv_menu_one, Color.parseColor("#66caff"));
            } else {
                helper.setTextColor(R.id.tv_menu_one, Color.parseColor("#707070"));
            }
        }
    }
}
